/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.anniversary.repository;

import me.zhengjie.modules.anniversary.domain.AssociationUserInfo;
import me.zhengjie.modules.anniversary.domain.Photos;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @website https://el-admin.vip
 * @author Jiayin Wei
 * @date 2020-07-27
 **/
public interface AssociationUserInfoRepository extends JpaRepository<AssociationUserInfo, Long>, JpaSpecificationExecutor<AssociationUserInfo> {
    @Query(value = "select a.*,u.username from gzmu_ann_association_user_info i,gzmu_ann_association a,sys_user u where i.user_id=u.user_id and i.association_id=a.id and u.user_id=:userId", nativeQuery = true)
    List<Map<String, Object>> getAssociationsByUserId(Long userId, Pageable pageable);

    @Query(value = "select a.*,u.username from gzmu_ann_association_user_info i,gzmu_ann_association a,sys_user u where i.user_id=u.user_id and i.association_id=a.id and u.user_id=:userId and a.type=:type", nativeQuery = true)
    List<Map<String, Object>> getAssociationsByType(Long userId, Integer type, Pageable pageable);

    @Query(value = "SELECT u.id,u.avatar_url headImg,u.username,b.name depart,i.type FROM sys_user u LEFT JOIN gzmu_ann_department b ON a.major_depart_id = b.id LEFT JOIN gzmu_ann_association_user_info i ON i.user_id = u.suer_id WHERE u.username LIKE ?1 AND u.is_enable=1",nativeQuery = true)
    List<Map<String,Object>> queryByName(String name, Pageable pageable);

    @Query(value = "SELECT DISTINCT type FROM gzmu_ann_association_user_info WHERE user_id = ?1",nativeQuery = true)
    String queryTypeByUserId(Long userId);

    @Query(value = "SELECT s.name,s.enter_date,d.name deptname FROM gzmu_ann_student s left join sys_dept d on d.dept_id=s.major_depart_id WHERE s.user_id=?1",nativeQuery = true)
    List<Map<String,Object>> queryStudentByUserId(Long userId);

    @Query(value = "SELECT name,work_date,remark FROM gzmu_ann_teacher WHERE user_id=?1",nativeQuery = true)
    List<Map<String, Object>> queryTeacherByUserId(Long userId);

    @Query(value = "SELECT u.username,u.avatar_url,t.gender,t.work_unit,t.phone,t.address,t.enter_date,d.`name` majorName FROM gzmu_ann_student t left join gzmu_ann_department d on d.id=t.major_depart_id left join sys_user u on u.user_id=t.user_id where u.user_id=?1",nativeQuery = true)
    List<Map<String,Object>> queryByUseridStuDetail(Long id);

    @Query(value = "SELECT u.username,u.avatar_url,t.gender,t.work_date,t.remark facName,t.work_unit,t.phone FROM gzmu_ann_teacher t left join gzmu_ann_department d on d.id=t.dept_id left join sys_user u on u.user_id=t.user_id where u.user_id=?1",nativeQuery = true)
    List<Map<String,Object>> queryByUseridTeaDetail(Long id);

    @Query(value = "SELECT u.user_id,u.avatar_url,au.type,u.username,t.remark,u.gender FROM gzmu_ann_association_user_info au LEFT JOIN sys_user u ON au.user_id = u.user_id LEFT JOIN gzmu_ann_teacher t ON u.user_id = t.user_id  WHERE au.association_id=:id AND u.username LIKE CONCAT('%',:userName,'%') AND au.is_enable = 1 AND u.verify_status=1",nativeQuery = true)
    List<Map<String,Object>> queryByAssociationId(Long id,String userName, Pageable pageable);

    @Query(value = "SELECT * FROM gzmu_ann_association_user_info WHERE association_id=?1 AND user_id=?2",nativeQuery = true)
    AssociationUserInfo queryByIdAndUserId(Long id, Long userId);

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM gzmu_ann_association_user_info WHERE id=?1", nativeQuery = true)
    void deleteUserInfoById(Long id);

    @Query(value = "SELECT * FROM gzmu_ann_photos WHERE ass_id=?1 and verify_status=1 and is_enable=1 ORDER BY create_time DESC",nativeQuery = true)
    List<Map<String,Object>> queryPhotoByAssId(Long id);

    /**
     * 校友会上传相册
     */
    @Transactional
    @Modifying
    @Query(value = " INSERT INTO gzmu_ann_photos(name,user_id,ass_id,type,time,path,verify_status,create_by) VALUES(:#{#photos.name},:#{#photos.userId},:#{#photos.assId},:#{#photos.type},:#{#photos.time},:#{#photos.path},:#{#photos.verifyStatus},:#{#photos.createBy}) ",nativeQuery = true)
    int insertPhotos(Photos photos);
}
